package com.janksenhu.compress;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
import org.tukaani.xz.FilterOptions;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.X86Options;
import org.tukaani.xz.XZOutputStream;

/**
 * Hello world!
 *
 */
public class App {

    public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
        System.out.println("Hello World!");
       
        File dir = new File("/Users/janksenhu/Documents/adt-bundle-mac-x86_64-20130522/sdk/platforms/android-17/android.jar");

        
        byte[] buf = new byte[8192];
        int blockSize = 1024 * 64;
        int left = blockSize;
        FileInputStream in = new FileInputStream(dir);
        
        //XZ压缩方法
        //IA64Options 因特尔64位架构 ，X86Options x86，32，64位架构
//        X86Options x86 = new X86Options();
//        LZMA2Options lzma2 = new LZMA2Options();
//        FilterOptions[] options = {x86, lzma2};
//        System.out.println("Encoder memory usage: "
//                + FilterOptions.getEncoderMemoryUsage(options)
//                + " KiB");
//        System.out.println("Decoder memory usage: "
//                + FilterOptions.getDecoderMemoryUsage(options)
//                + " KiB");
//        FileOutputStream outfile = new FileOutputStream("/Users/janksenhu/NetBeansProjects/javaCompress/target/foo.xz");
//        XZOutputStream outxz = new XZOutputStream(outfile, options);
//        long begintime=System.currentTimeMillis();
//        while (true) {
//            int size = in.read(buf, 0, Math.min(buf.length, left));
//            if (size == -1) {
//                break;
//            }
//
//            outxz.write(buf, 0, size);
//            left -= size;
//
//            if (left == 0) {
//                outxz.endBlock();
//                left = blockSize;
//            }
//        }
//        outxz.finish();
//        long endtime=System.currentTimeMillis();
//        System.out.println("time is "+(endtime-begintime));
//        
        
        //pack200压缩方式
//        FileOutputStream outfilePack = new FileOutputStream("/Users/janksenhu/NetBeansProjects/javaCompress/target/foo.pack");
//        Pack200CompressorOutputStream packOut = new Pack200CompressorOutputStream(outfilePack);
//        long begintimeP=System.currentTimeMillis();
//        while (true) {
//            int size = in.read(buf);
//            if (size == -1) {
//                break;
//            }
//            packOut.write(buf, 0, size);
//        }
//        packOut.finish();
//        packOut.close();
//        long endtimeP=System.currentTimeMillis();
//        System.out.println("time is "+(endtimeP-begintimeP));
        
        
        //Bzip2压缩方式
//        FileOutputStream outfileBz = new FileOutputStream("/Users/janksenhu/NetBeansProjects/javaCompress/target/foo.bz2");
//        BZip2CompressorOutputStream bzout = new BZip2CompressorOutputStream(outfileBz);
//        long begintimeB=System.currentTimeMillis();
//        while (true) {
//            int size = in.read(buf);
//            if (size == -1) {
//                break;
//            }
//            bzout.write(buf, 0, size);
//        }
//        bzout.finish();
//        bzout.flush();
//        bzout.close();
//        long endtimeB=System.currentTimeMillis();
//        System.out.println("time is "+(endtimeB-begintimeB));
       
       
        
        //使用Gzip压缩
//        FileOutputStream outfilegz = new FileOutputStream("/Users/janksenhu/NetBeansProjects/javaCompress/target/foo.gz");
//        GzipCompressorOutputStream outGzip = new GzipCompressorOutputStream(outfilegz);
//        long begintimeG=System.currentTimeMillis();
//        while (true) {
//            int size = in.read(buf);
//            if (size == -1) {
//                break;
//            }
//            outGzip.write(buf, 0, size);
//        }
//        
//        outGzip.flush();
//        outGzip.close();
//        long endtimeG=System.currentTimeMillis();
//        System.out.println("time is "+(endtimeG-begintimeG));
        
        
        //xz压缩
        FileOutputStream xz = new FileOutputStream("/Users/janksenhu/NetBeansProjects/javaCompress/target/foo.xz");
        XZCompressorOutputStream xzou = new XZCompressorOutputStream(xz);
        long begintimeX=System.currentTimeMillis();
        while (true) {
            int size = in.read(buf);
            if (size == -1) {
                break;
            }
            xzou.write(buf, 0, size);
        }
        
        xzou.flush();
        xzou.close();
        long endtimeX=System.currentTimeMillis();
        System.out.println("time is "+(endtimeX-begintimeX));

    }
}